package day_2022_1_to_2.leetcode;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

public class Num2 {
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode head=new ListNode(0);
            ListNode next;
            ListNode last=head;
            head.next=l1;
            head=head.next;
            while (l1.next!=null && l2.next!=null){
                head.val=l1.val+l2.val;
                l1=l1.next;
                l2=l2.next;

                next=head.next;
                head=next;
            }

            if(l1!=null){
                head.next=l1;
            }else if(l2!=null){
                head.next=l2;
            }
            return last.next;
        }
    }
}
